package com.xbk.seckill.order.infrastructure.repository;

import com.xbk.seckill.common.exception.ErrorCode;
import com.xbk.seckill.common.exception.SeckillException;
import com.xbk.seckill.order.domain.model.entity.SeckillOrder;
import com.xbk.seckill.order.domain.repository.SeckillOrderRepository;
import com.xbk.seckill.order.infrastructure.mapper.SeckillGoodsOrderMapper;
import com.xbk.seckill.order.infrastructure.mapper.SeckillOrderMapper;
import com.xbk.seckill.order.infrastructure.mapper.SeckillUserOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 秒杀订单
 *
 * @author xiexu
 * @date 2025/1/12 22:51
 */
@Repository
public class SeckillOrderRepositoryImpl implements SeckillOrderRepository {

    @Autowired
    private SeckillUserOrderMapper seckillUserOrderMapper;

    @Autowired
    private SeckillGoodsOrderMapper seckillGoodsOrderMapper;

    /**
     * 保存订单
     *
     * @param seckillOrder 订单
     * @return 是否保存成功
     */
    @Override
    public boolean saveSeckillOrder(SeckillOrder seckillOrder) {
        if (Objects.isNull(seckillOrder)) {
            throw new SeckillException(ErrorCode.PARAMS_INVALID);
        }
        // 保存订单到用户订单表
        int userResult = seckillUserOrderMapper.saveSeckillOrder(seckillOrder);
        // 保存订单到商品订单表
        int goodsResult = seckillGoodsOrderMapper.saveSeckillOrder(seckillOrder);
        return userResult == 1 && goodsResult == 1;
    }

    /**
     * 根据用户id获取订单列表
     *
     * @param userId 用户id
     * @return 订单列表
     */
    @Override
    public List<SeckillOrder> getSeckillOrderByUserId(Long userId) {
        if (Objects.isNull(userId)) {
            throw new SeckillException(ErrorCode.PARAMS_INVALID);
        }
        return seckillUserOrderMapper.getSeckillOrderByUserId(userId);
    }

    /**
     * 根据商品id获取订单列表
     *
     * @param goodsId 商品id
     * @return 订单列表
     */
    @Override
    public List<SeckillOrder> getSeckillOrderByGoodsId(Long goodsId) {
        if (Objects.isNull(goodsId)) {
            throw new SeckillException(ErrorCode.PARAMS_INVALID);
        }
        return seckillGoodsOrderMapper.getSeckillOrderByGoodsId(goodsId);
    }

    /**
     * 以userId作为分片键删除订单
     *
     * @param orderId 订单id
     * @param userId  用户id
     */
    @Override
    public void deleteOrderShardingUserId(Long orderId, Long userId) {
        if (Objects.isNull(orderId) || Objects.isNull(userId)) {
            throw new SeckillException(ErrorCode.PARAMS_INVALID);
        }
        seckillUserOrderMapper.deleteOrder(userId, orderId);
    }

    /**
     * 以goodsId作为分片键删除订单
     *
     * @param orderId 订单id
     * @param goodsId 商品id
     */
    @Override
    public void deleteOrderShardingGoodsId(Long orderId, Long goodsId) {
        if (Objects.isNull(orderId) || Objects.isNull(goodsId)) {
            throw new SeckillException(ErrorCode.PARAMS_INVALID);
        }
        // 删除订单
        seckillGoodsOrderMapper.deleteOrder(goodsId, orderId);
    }
}
